﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MuaDeal
{
    public partial class DatHang : Form
    {
        public DatHang()
        {
            InitializeComponent();
        }
        DataTable loaideal = new DataTable();
        private string tenkh;
        public string Tenkh
        {
            get { return tenkh; }
            set { tenkh = value; }
        }
        DataTable dt = new DataTable();
        public void loadlai()
        {
            lstdsdeal.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Text = row[0].ToString();
                item.SubItems.Add(row[2].ToString());
                item.SubItems.Add(row[5].ToString());
                item.SubItems.Add(row[6].ToString());
                item.SubItems.Add(row[7].ToString());
                item.SubItems.Add(row[3].ToString());
                item.SubItems.Add(row[4].ToString());
                item.SubItems.Add(row[10].ToString());
                lstdsdeal.Items.Add(item);
            }
        }
        private void DatHang_Load(object sender, EventArgs e)
        { 
            #region THONG TIN KHACH HANG
            try
            {
               
                Datahelper helper = new Datahelper();
                helper.Open();
                DataTable dttt = new DataTable();
                SqlCommand comman = new SqlCommand();

                comman.CommandText = "LayThongTinUserTheoMa";
                comman.CommandType = CommandType.StoredProcedure;
                SqlParameter bien1 = comman.Parameters.Add("@tendn", SqlDbType.Char, 30);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = this.Tenkh;

                comman.Connection = helper.Connection;
                SqlDataAdapter adt = new SqlDataAdapter(comman);
                adt.Fill(dttt);
                string proc = "sp_LayDSLinhVuc";
                loaideal = helper.ExecuteQueryStore(proc);
                string proc1 = "sp_LayDanhSachSanPham";
                dt = helper.ExecuteQueryStore(proc1);
                helper.Close();
                if (dt.Rows.Count > 0)
                {
                    btnttkhang.Text = dttt.Rows[0][3].ToString();
                    lbsotienconlai2.Text = dttt.Rows[0][7].ToString();
                    
                }
                cmbloaideal.DataSource = loaideal;
                cmbloaideal.DisplayMember = loaideal.Columns[1].ToString();
                cmbloaideal.ValueMember = loaideal.Columns[0].ToString();
                loadlai();
            }
            catch (SqlException ex) { }
            #endregion


        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnxemdssp_Click(object sender, EventArgs e)
        {
            #region XEM LAI
            try
            {

                Datahelper helper = new Datahelper();
                helper.Open();
                string proc1 = "sp_LayDanhSachSanPham";
                if (radDirty.Checked == true)
                {
                    proc1 = "dbo.sp_LayDSSP_DirtyRead";
                }
                else if (radPhantom.Checked == true)
                {
                    proc1 = "dbo.sp_LayDSSP_Phantom";
                }
                else if (radPT.Checked == true)
                {
                    proc1 = "dbo.sp_LayDSSP_Phantom_RS";
                }
                dt = helper.ExecuteQueryStore(proc1);
                helper.Close();
                
                loadlai();
            }
            catch (SqlException ex) { }
            #endregion
        }

        private void btxem_Click(object sender, EventArgs e)
        {
            #region HIEN THI THEO LOAI
            try
            {

                Datahelper helper = new Datahelper();
                helper.Open();
                int loai = int.Parse(this.cmbloaideal.SelectedValue.ToString());

                // gọi store có tham số
                SqlCommand comman = new SqlCommand();
                comman.CommandText = "sp_TimKiemSPTheoLoai";
                comman.CommandType = CommandType.StoredProcedure;
                SqlParameter bien1 = comman.Parameters.Add("@loai", SqlDbType.Int);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = loai;
                comman.Connection = helper.Connection;

                //lấy ds trả về gán vào listview
                DataTable dttimkiem = new DataTable();
                SqlDataReader reader = comman.ExecuteReader(); 
                lstdsdeal.Items.Clear();
                while (reader.Read())
                {
          
                    
                        ListViewItem item = new ListViewItem();
                        item.Text = reader[0].ToString();
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        item.SubItems.Add(reader[6].ToString());
                        item.SubItems.Add(reader[7].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[10].ToString());
                        lstdsdeal.Items.Add(item);
                    
                }
                helper.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Lỗi  " + ex.Message);
            }
            #endregion
        }

        private void lstdsdeal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstdsdeal.SelectedIndices.Count > 0)
            {
                txtmadeal.Text = lstdsdeal.Items[lstdsdeal.SelectedIndices[0]].Text;
                lbgiaban1.Text = lstdsdeal.Items[lstdsdeal.SelectedIndices[0]].SubItems[5].Text;
            }
        }
    }
}